﻿using System;

namespace TBM.Core
{
    /// <summary>
    /// Command 变量类型
    /// </summary>
    public class CommandParamType
    {
        /// <summary>
        /// 整型
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static object Int(object value)
        {
            int _value;

            if (value != null && int.TryParse(value.ToString(), out _value))
            {
                return _value;
            }
            else
            {
                return DBNull.Value;
            }
        }

        /// <summary>
        /// 长整型
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static object Long(object value)
        {
            long _value;

            if (value != null && long.TryParse(value.ToString(), out _value))
            {
                return _value;
            }
            else
            {
                return DBNull.Value;
            }
        }

        /// <summary>
        /// 单精度型
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static object Float(object value)
        {
            float _value;

            if (value != null && float.TryParse(value.ToString(), out _value))
            {
                return _value;
            }
            else
            {
                return DBNull.Value;
            }
        }

        /// <summary>
        /// 双精度型
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static object Double(object value)
        {
            double _value;

            if (value != null && double.TryParse(value.ToString(), out _value))
            {
                return _value;
            }
            else
            {
                return DBNull.Value;
            }
        }

        /// <summary>
        /// 数值型
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static object Decimal(object value)
        {
            decimal _value;

            if (value != null && decimal.TryParse(value.ToString(), out _value))
            {
                return _value;
            }
            else
            {
                return DBNull.Value;
            }
        }

        /// <summary>
        /// 布尔型
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static object Boolean(object value)
        {
            bool _value;

            if (value != null && bool.TryParse(value.ToString(), out _value))
            {
                return _value;
            }
            else
            {
                return DBNull.Value;
            }
        }

        /// <summary>
        /// DBNull.Value或者返回字符串
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static object NullOrString(object value)
        {
            if (value != null)
            {
                return value.ToString();
            }
            else
            {
                return DBNull.Value;
            }
        }

        /// <summary>
        /// 请求的值是否为空
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsRequestNullOrEmpty(object value)
        {
            return value == null || String.IsNullOrEmpty(value.ToString());
        }
    }
}